

set more off
mat drop _all


*** cleaning (basically the same as 0_cleaning, but this standardise the scores)

use "$path_data/data/temp/main_reg_data.dta", clear

replace evaluation_el=5 if evaluation_el==7
***** Teacher evaluation
replace evaluation_el = 5 if evaluation_el>5 & !missing(evaluation_el)
gen DT_per_min1 = DTSbl_per_min_P2 if grade==2
replace DT_per_min1 = DTSbl_per_min_P3 if grade==4
gen DT_per_min2 = DTSel_per_min_P2 if grade==2
replace DT_per_min2 = DTSel_per_min_P3 if grade==4
gen DT_score1 = score_DTScoreP2 if grade==2
replace DT_score1 = score_DTScoreP3 if grade==4
gen DT_score2 = score_Post_DTScoreP2 if grade==2
replace DT_score2 = score_Post_DTScoreP3 if grade==4
gen DT_time1 = time_DTScoreP2 if grade==2
replace DT_time1 = time_DTScoreP3 if grade==4
gen DT_time2 = time_Post_DTScoreP2 if grade==2
replace DT_time2 = time_Post_DTScoreP3 if grade==4

centile ptsbl_overall_score, centile(20 40 60 80)
replace ptsbl_overall_score=5 if ptsbl_overall_score<=r(c_1)
replace ptsbl_overall_score=4 if ptsbl_overall_score<=r(c_2) & ptsbl_overall_score>r(c_1)
replace ptsbl_overall_score=3 if ptsbl_overall_score<=r(c_3) & ptsbl_overall_score>r(c_2)
replace ptsbl_overall_score=2 if ptsbl_overall_score<=r(c_4) & ptsbl_overall_score>r(c_3)
replace ptsbl_overall_score=1 if ptsbl_overall_score>r(c_4)

centile ptsel_overall_score, centile(20 40 60 80)
replace ptsel_overall_score=5 if ptsel_overall_score<=r(c_1)
replace ptsel_overall_score=4 if ptsel_overall_score<=r(c_2) & ptsel_overall_score>r(c_1)
replace ptsel_overall_score=3 if ptsel_overall_score<=r(c_3) & ptsel_overall_score>r(c_2)
replace ptsel_overall_score=2 if ptsel_overall_score<=r(c_4) & ptsel_overall_score>r(c_3)
replace ptsel_overall_score=1 if ptsel_overall_score>r(c_4)

centile DT_per_min1, centile(20 40 60 80)
replace DT_per_min1=5 if DT_per_min1<=r(c_1)
replace DT_per_min1=4 if DT_per_min1<=r(c_2) & DT_per_min1>r(c_1)
replace DT_per_min1=3 if DT_per_min1<=r(c_3) & DT_per_min1>r(c_2)
replace DT_per_min1=2 if DT_per_min1<=r(c_4) & DT_per_min1>r(c_3) // Hikaru Modified here (30/07/2020).
replace DT_per_min1=1 if DT_per_min1>r(c_4)

centile DT_per_min2, centile(20 40 60 80)
replace DT_per_min2=5 if DT_per_min2<=r(c_1)
replace DT_per_min2=4 if DT_per_min2<=r(c_2) & DT_per_min2>r(c_1)
replace DT_per_min2=3 if DT_per_min2<=r(c_3) & DT_per_min2>r(c_2)
replace DT_per_min2=2 if DT_per_min2<=r(c_4) & DT_per_min2>r(c_3)
replace DT_per_min2=1 if DT_per_min2>r(c_4)


centile DT_score1, centile(20 40 60 80)
replace DT_score1=5 if DT_score1<=r(c_1)
replace DT_score1=4 if DT_score1<=r(c_2) & DT_score1>r(c_1)
replace DT_score1=3 if DT_score1<=r(c_3) & DT_score1>r(c_2)
replace DT_score1=2 if DT_score1<=r(c_4) & DT_score1>r(c_3) // Hikaru Modified here (30/07/2020).
replace DT_score1=1 if DT_score1>r(c_4)

centile DT_score2, centile(20 40 60 80)
replace DT_score2=5 if DT_score2<=r(c_1)
replace DT_score2=4 if DT_score2<=r(c_2) & DT_score2>r(c_1)
replace DT_score2=3 if DT_score2<=r(c_3) & DT_score2>r(c_2)
replace DT_score2=2 if DT_score2<=r(c_4) & DT_score2>r(c_3)
replace DT_score2=1 if DT_score2>r(c_4)


rename ptsbl_overall_score pts_overall_score_1
rename ptsel_overall_score pts_overall_score_2
rename evaluation_bl eval_adjust1
rename evaluation_el eval_adjust2


rename DT_score_pre score_adjust1
rename DT_score_post score_adjust2

keep DT_per_min1 DT_per_min2 DT_score1 DT_score2 DT_time1 DT_time2 ///
pts_overall_score_1 pts_overall_score_2 eval_adjust1 eval_adjust2 ///
treatment student_no school_no grade branch* student_gender wrongDT ///
score_adjust1 score_adjust2
reshape long DT_per_min DT_score DT_time pts_overall_score_ eval_adjust score_adjust, i(student_no) j(endline)
drop if missing(eval_adjust)
replace endline=0 if endline==1
replace endline=1 if endline==2

gen did = treatment*endline

gen dif_eval_dt = abs(eval_adjust-DT_per_min)
gen dif_eval_pt = abs(eval_adjust-pts_overall_score_)
gen dif_eval_score = abs(eval_adjust-score_adjust)

gen con_bl = cond(treatment==0 & endline==0,1,0)
gen con_el = cond(treatment==0 & endline==1,1,0)
gen tr_bl = cond(treatment==1 & endline==0,1,0)
gen tr_el = cond(treatment==1 & endline==1,1,0)

gen control_bl = con_bl*eval_adjust
gen control_el = con_el*eval_adjust
gen treat_bl = tr_bl*eval_adjust
gen treat_el = tr_el*eval_adjust

*** correlation
preserve
drop if missing(DT_per_min)|missing(eval_adjust)| wrongDT == 1
bys student_no: gen temp=_N
drop if temp==1
reg DT_per_min con_bl con_el tr_bl tr_el control_bl control_el treat_bl treat_el, noconstant cluster(school_no)
*outreg2 using "temp/teach_student_corr.xls", replace ctitle(DT_all_sample)
test treat_bl=control_bl
test treat_el=control_el
*test treat_el-treat_bl=control_el-control_bl
reg dif_eval_dt did treatment endline, cluster(school_no)
*outreg2 using "temp/teach_student_corr.xls", pvalue append ctitle(dif_dt)

	matrix TS1_DT_temp = r(table)
	matrix TS1_DT_r2 = e(r2)
	matrix TS1_DT_obs = e(N)

	cgmwildboot_2 dif_eval_dt did treatment endline, cluster(school_no) bootcluster(school_no) null(0 0 0) reps(999) seed(1)
	matrix TS1_DT_temp_p = e(Pvals)

	
	foreach s in be se pv rpv wpv {
		matrix TS1_DT_`s' = J(4,1,.)
	}

	foreach j in 1 2 3 4 {
		* beta
		matrix TS1_DT_be[`j',1] = TS1_DT_temp[1,`j']
		* standard error
		matrix TS1_DT_se[`j',1] = TS1_DT_temp[2,`j']
		* p value
		matrix TS1_DT_pv[`j',1] = TS1_DT_temp[4,`j']
		matrix TS1_DT_wpv[`j',1] = TS1_DT_temp_p[`j',1]
	}




*outreg2 using "temp/teach_student_corr.xls", pvalue append ctitle(dif_dt)
restore




preserve
drop if missing(pts_overall_score_)|missing(eval_adjust) | dif_eval_pt == .
bys student_no: gen temp=_N
drop if temp==1
reg dif_eval_pt did treatment endline, cluster(school_no)

	matrix TS1_PTS_temp = r(table)
	matrix TS1_PTS_r2 = e(r2)
	matrix TS1_PTS_obs = e(N)

	cgmwildboot_2 dif_eval_pt did treatment endline, cluster(school_no) bootcluster(school_no) null(0 0 0) reps(999) seed(1)
	matrix TS1_PTS_temp_p = e(Pvals)

	
	foreach s in be se pv rpv wpv {
		matrix TS1_PTS_`s' = J(4,1,.)
	}

	foreach j in 1 2 3 4 {
		* beta
		matrix TS1_PTS_be[`j',1] = TS1_PTS_temp[1,`j']
		* standard error
		matrix TS1_PTS_se[`j',1] = TS1_PTS_temp[2,`j']
		* p value
		matrix TS1_PTS_pv[`j',1] = TS1_PTS_temp[4,`j']
		matrix TS1_PTS_wpv[`j',1] = TS1_PTS_temp_p[`j',1]
	}



restore




*** correlation
preserve
drop if missing(DT_per_min)|missing(eval_adjust)| wrongDT == 1 | dif_eval_score == .
bys student_no: gen temp=_N
drop if temp==1


reg dif_eval_score did treatment endline, cluster(school_no)

	matrix TS1_score_temp = r(table)
	matrix TS1_score_r2 = e(r2)
	matrix TS1_score_obs = e(N)

	cgmwildboot_2 dif_eval_score did treatment endline, cluster(school_no) bootcluster(school_no) null(0 0 0) reps(999) seed(1)
	matrix TS1_score_temp_p = e(Pvals)

	
	foreach s in be se pv rpv wpv {
		matrix TS1_score_`s' = J(4,1,.)
	}

	foreach j in 1 2 3 4 {
		* beta
		matrix TS1_score_be[`j',1] = TS1_score_temp[1,`j']
		* standard error
		matrix TS1_score_se[`j',1] = TS1_score_temp[2,`j']
		* p value
		matrix TS1_score_pv[`j',1] = TS1_score_temp[4,`j']
		matrix TS1_score_wpv[`j',1] = TS1_score_temp_p[`j',1]
	}




restore






foreach s in be se pv r2 rpv obs wpv {
	matrix TS1_`s' = TS1_DT_`s', TS1_PTS_`s', TS1_score_`s'
}










forvalues item = 1/4 {
		forvalues scale = 1/3 {
		if TS1_wpv[`item',`scale']<=0.01 {
			local star_`item'`scale' %3s "***"
		}
		else if (TS1_wpv[`item', `scale']>0.01) & (TS1_wpv[`item', `scale']<=0.05) {
			local star_`item'`scale' %2s "**"
		}
		else if (TS1_wpv[`item', `scale']>0.05) & (TS1_wpv[`item', `scale']<=0.10) {
			local star_`item'`scale' %1s "*"
		}
		else {
			local star_`item'`scale'  ""
		}
	} // forvalues `scale' loop

}  // forvalues `item' loop




forvalues item = 4/4 {
		forvalues scale = 1/3 {
		if TS1_pv[`item',`scale']<=0.01 {
			local star_`item'`scale'_a %3s "***"
		}
		else if (TS1_pv[`item', `scale']>0.01) & (TS1_pv[`item', `scale']<=0.05) {
			local star_`item'`scale'_a %2s "**"
		}
		else if (TS1_pv[`item', `scale']>0.05) & (TS1_pv[`item', `scale']<=0.10) {
			local star_`item'`scale'v %1s "*"
		}
		else {
			local star_`item'`scale'v  ""
		}
	} // forvalues `scale' loop

}  // forvalues `item' loop



tempname hh
file open `hh' using "$pardir/table6.tex", write replace
file write `hh' "" _newline
file write `hh' "% Date: `c(current_date)'" _newline
file write `hh' "% Time: `c(current_time)'" _newline
file write `hh' "" _newline



file write `hh' "\begin{table}[t!]" _newline
file write `hh' "\centering" _newline
file write `hh' "\caption{Association between Teachers' Assessment and Student Performance}" _newline
file write `hh' "\label{tab:TAA11}" _newline
file write `hh' "\scalebox{0.7}{" _newline
file write `hh' "\begin{threeparttable}" _newline
file write `hh' "\begin{tabular}{lcc}\toprule" _newline
file write `hh' "Dependent Variable & \multicolumn{2}{c}{Absolute Difference between Teachers''} \\"_newline
file write `hh' "& \multicolumn{2}{c}{Perception and Student' Scores} \\"_newline
file write `hh' "& DT Score\textsuperscript{a} & PTSII-C Score\textsuperscript{b} \\" _newline
file write `hh' "               &              (1)                        &         (2)                               \\\midrule\midrule" _newline
file write `hh' "Treatment $\times$ endline 				   	   &  " %04.3f (TS1_be[1,3]) `star_13' "   &  " %04.3f (TS1_be[1,2]) `star_12' "   \\" _newline
file write `hh' "                           				   	   & (" %04.3f (TS1_se[1,3])           ")  & (" %04.3f (TS1_se[1,2])           ")  \\" _newline
file write `hh' "Treatment                  				   	   &  " %04.3f (TS1_be[2,3]) `star_23' "   &  " %04.3f (TS1_be[2,2]) `star_22' "   \\" _newline
file write `hh' "                           				   	   & (" %04.3f (TS1_se[2,3])           ")  & (" %04.3f (TS1_se[2,2])           ")  \\" _newline
file write `hh' "Endline                    				   	   &  " %04.3f (TS1_be[3,3]) `star_33' "   &  " %04.3f (TS1_be[3,2]) `star_32' "   \\" _newline
file write `hh' "                           				   	   & (" %04.3f (TS1_se[3,3])           ")  & (" %04.3f (TS1_se[3,2])           ")  \\" _newline
*file write `hh' "Constant                   				   	   &  " %04.3f (TS1_be[4,3]) `star_43' "   &  " %04.3f (TS1_be[4,2]) `star_42' "   \\" _newline
file write `hh' "Constant\textsuperscript{c}                   				   	   &  " %04.3f (TS1_be[4,3]) `star_43_a' "   &  " %04.3f (TS1_be[4,2])   `star_42_a'  "  \\" _newline
file write `hh' "                           				   	   & (" %04.3f (TS1_se[4,3])           ")  & (" %04.3f (TS1_se[4,2])           ")  \\" _newline
file write `hh' "                                                  &                                          &                                          \\\midrule" _newline
file write `hh' "Num of Obs.                                       &  "      (TS1_obs[1,3])            "   &  "      (TS1_obs[1,2])            "   \\" _newline
file write `hh' "R-squared                                         &  " %04.3f (TS1_r2[1,3])           "   &  " %04.3f (TS1_r2[1,2])           "   \\\bottomrule" _newline
file write `hh' "\end{tabular}" _newline
file write `hh' "\end{threeparttable}" _newline
file write `hh' "}" _newline
file write `hh' "  \label{tab:addlabel}%" _newline
file write `hh' "\end{table}" _newline






file write `hh' "" _newline
file write `hh' "" _newline
file write `hh' "" _newline
file write `hh' "" _newline

file close `hh'















